package reflection;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * 获得页面上的资源
 * User:bob.yang
 * Date 2015/8/25 0025
 */
public class GetResource {

    public static void main(String []args) throws IOException {
        //获得一个默认的HttpClient客户端
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(5000)
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .setCookieSpec(CookieSpecs.STANDARD_STRICT)
                .build();
        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultRequestConfig(defaultRequestConfig)
                .build();

        //获得目标页面
        String url = "http://book.douban.com/subject/20427187/?from=tag";
//        File file = new File("D://goUrl.text");
//        FileReader fileReader = new FileReader(file);
//        BufferedReader reader = new BufferedReader(fileReader);
//        String str = null;
//        while((str = reader.readLine()) != null) {
//
//        }





        //模拟浏览器
        HttpGet httpGet =   new HttpGet(url);
        httpGet.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        httpGet.setHeader("Accept-Encoding","gzip,deflate,sdch");
        httpGet.setHeader("Accept-Language","zh-CN,zh;q=0.8");
        httpGet.setHeader("Connection","keep-alive");
        httpGet.setHeader("Cache-Control","max-age=0");
//                httpGet.setHeader("Host","movie.douban.com");
        httpGet.setHeader("Cookie", "bid=\"vsXMg4Bay30\"; ll=\"108296\"; _ga=GA1.2.1038398848.1430800075; ps=y; viewed=\"26569724_26390842_26451423_5975576_26392727_25773187_26413027_4010969_24722612_26274206\"; _pk_ref.100001.8cb4=%5B%22%22%2C%22%22%2C1440396583%2C%22http%3A%2F%2Faccounts.douban.com%2Flogin%3Fuid%3D51295828%26alias%3D1245964132%2540qq.com%26redir%3Dhttp%253A%252F%252Fwww.douban.com%252F%26source%3Dindex_nav%26error%3D1013%22%5D; ue=\"1245964132@qq.com\"; dbcl2=\"51295828:5qKM2LBoMeY\"; ck=\"YWLZ\"; ap=1; push_noty_num=0; push_doumail_num=0; _pk_id.100001.8cb4=84049da8a64e0f42.1438736478.36.1440407338.1440392367.; _pk_ses.100001.8cb4=*; __utma=30149280.1038398848.1430800075.1440391513.1440396243.37; __utmb=30149280.68.10.1440396243; __utmc=30149280; __utmz=30149280.1439520983.20.8.utmcsr=douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmv=30149280.5129");
        httpGet.setHeader("Upgrade-Insecure-Requests", "1");
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36");

        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpGet);

        //判断返回的状态
        if(response.getStatusLine().getStatusCode() != 200) {
            System.out.println("状态："+response.getStatusLine());
            return;
        }
        getResource(response.getEntity());
        response.close();
    }


    //抓取需要的资源
    public static void getResource(HttpEntity entity) throws IOException {
        String webContent = EntityUtils.toString(entity);
        //首先抓取需要的图片
        Document doc = Jsoup.parse(webContent);
        Elements elements = doc.select("div#mainpic"); //先根据id选择相应的元素
        doc = Jsoup.parse(elements.toString());
        Elements elements1 = doc.select("a[href]"); //再在相应的元素内选择想要的元素
        for(Element node : elements1){
            String href = node.attr("href"); //把元素的内容取出来
            System.out.println("图片的地址是："+href);
        }
        //然后再抓取图书的介绍
        doc = Jsoup.parse(webContent);
        elements = doc.select("div#info");
        for(Element node : elements) {
            String content = node.text(); //获得该元素下的文本信息
            String[] array = content.split(" ");
            int i = 0;
            for(String str : array) {
                System.out.println(str + "-----" +i);
                i ++;
            }
        }

        //抓取书名
        doc = Jsoup.parse(webContent);
        elements = doc.select("h1");
        for(Element node : elements) {
            String content = node.text(); //获得该元素下的文本信息
            System.out.println("书名："+content);
        }

        //抓入图书的简介
        doc = Jsoup.parse(webContent);
        elements = doc.select("div#link-report");
        doc = Jsoup.parse(elements.toString());
        elements = doc.select("span[class=all hidden]"); //根据元素的属性值拉取内容
        for(Element node : elements) {
            String content = node.text(); //获得该元素下的文本信息
            String[] array = content.split(" ");
            int i = 0;
            for(String str : array) {
                System.out.println(str);
            }
        }
        //如果没有扩展项的情况下
        if(elements.size() == 0) {
            doc = Jsoup.parse(webContent);
            elements = doc.select("div#link-report");
            doc = Jsoup.parse(elements.toString());
            elements = doc.select("div[class=intro]"); //根据元素的属性值拉取内容
            for(Element node : elements) {
                String content = node.text(); //获得该元素下的文本信息
                String[] array = content.split(" ");
                int i = 0;
                for (String str : array) {
                    System.out.println(str);
                }
            }
        }
    }
}
